package com.luxury.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.luxury.R;
import com.luxury.SlidingMenu;
import com.luxury.adpter.HomeRecyclerAdapter;
import com.luxury.bean.HomeBean;
import com.luxury.decoration.SpacesItemDecoration;
import com.luxury.r_innerface.InterfaceLuxury;
import com.luxury.utils.URLContants;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by Administrator on 2016/12/5.
 */

public class HomeFragment extends Fragment {
    @BindView(R.id.iv_title_homefragment)
    ImageView iv_TitleHomefragment;
    @BindView(R.id.iv_search_homefragment)
    ImageView iv_SearchHomefragment;
    @BindView(R.id.iv_navagation_homefragment)
    ImageView iv_NavagationHomefragment;
    @BindView(R.id.rv_homefragment)
    RecyclerView rv_Homefragment;
    private Context mContext;
    private SlidingMenu mMenu;
    private HomeRecyclerAdapter mAdapter;
    private List<HomeBean.FloorsBean> mListData = new ArrayList<>();
    public static final String BAS_EURL = "";
    public static final String TAG = "homefragment";

    public static HomeFragment newInstance() {
        Bundle args = new Bundle();
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    private void initData() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(URLContants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create()).build();
        InterfaceLuxury iLuxury = retrofit.create(InterfaceLuxury.class);
        Call<HomeBean> homeBean_Floors = iLuxury.getHomeBean();
        homeBean_Floors.enqueue(new Callback<HomeBean>() {
            @Override
            public void onResponse(Call<HomeBean> call, Response<HomeBean> response) {
                if (response.isSuccessful()) {
//                   数据源
//                    mListData = response.body();
                    HomeBean body = response.body();
                    mListData = body.getFloors();
                    mAdapter = new HomeRecyclerAdapter(mContext, mListData);
                    rv_Homefragment.setAdapter(mAdapter);
                }
            }

            @Override
            public void onFailure(Call<HomeBean> call, Throwable t) {

            }
        });


    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
//        得到主界面中的侧滑控件
        mMenu = (SlidingMenu) getActivity().findViewById(R.id.slidingmenu_main);

        ButterKnife.bind(this, view);
        initRecyclerView();
        initData();
//        给侧滑按钮设置点击事件
        iv_NavagationHomefragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                侧滑菜单出来的方法
                mMenu.toggle();
            }
        });

        return view;
    }

    private void initRecyclerView() {
        rv_Homefragment.setHasFixedSize(true);
        rv_Homefragment.addItemDecoration(new SpacesItemDecoration(0));
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        rv_Homefragment.setLayoutManager(manager);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}
